<?php
/*
 * MachineReactivate.php
 * Reactivate an inactive machine in the database
 * 
 * Copyright 2013 Eugene Mah <eugenemah@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

require('functions.inc');

/*
 * Establish a connection to the database using PDO. Most of this is
 * taken from PHP in Action by Reiersol, Baker, Shiflett (Manning Publications)
 * and the PHP PDO documentation (http://php.net/manual/en/book.pdo.php)
 */
try {
  $conn = new PDO(
    'mysql:dbname=ComplianceReports;host=localhost',
    'rad','tfFxn2Tley');
}
catch (Exception $e) {
  throw new Exception($e->getMessage());
}
// Set PDO to throw more exceptions when encountered
$conn->setAttribute(PDO::ATTR_ERRMODE,
                    PDO::ERRMODE_EXCEPTION);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title>Reactivate Machine</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta name="generator" content="Geany 1.23.1" />
</head>

<body>
  <h1>Radiological Equipment Database</h1>
  <h2>Reactivate machine</h2>
<?php
if (isset($_POST['MachineID'])) {
  $MachID = strip_tags($_POST['MachineID']);
  if (isset($_POST['Note'])) $Note = strip_tags($_POST['Note']);
  else $Note = "\n";
  $query_reactivate = $conn->prepare("UPDATE Machines SET Status=\"Active\", Notes=concat(Notes,\"\n\", :Note) WHERE MachineID=:MachineID");
  $query_reactivate->bindParam('MachineID',$MachID,PDO::PARAM_INT);
  $query_reactivate->bindParam('Note',$Note,PDO::PARAM_STR);
  $query_reactivate->execute();
  echo "<p>Machine " . $MachID . " reactivated</p>";
}
?>
  <form action="MachineReactivate.php" method="POST">
    <p>Select an inactive machine: 
<?php
// Get a list of inactive machines from the database
$query_inactive = $conn->prepare("SELECT MachineID,Description from Machines where Status=\"Inactive\" order by Description");
$query_inactive->execute();

$machSelBox="<select name=\"MachineID\" size=1>\n"; 
$machSelBox .= "<option value=\"0\">Select a machine</option>\n";
while ($row = $query_inactive->fetch(PDO::FETCH_ASSOC)) { 
  $machSelBox.="<option value=\"" . $row['MachineID'] . "\">" . $row['Description'] . "</option>\n"; 
} 
$machSelBox.="</select>\n"; 
echo $machSelBox;
?>
    </p>
    <textarea name="Note" rows="3" cols="70">Enter a note</textarea>
    <p><input type="submit" value="Reactivate" />  / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
  </form>

</body>

</html>
